package com.genius.controller;

import com.genius.annotation.PreFuncAuth;
import com.genius.core.JSONResult;
import com.genius.utils.PageUtils;
import com.genius.utils.SecurityUtils;
import com.genius.workflow.bo.CompleteTaskBO;
import com.genius.workflow.bo.SelectTaskByPageBO;
import com.genius.workflow.vo.ProcessTaskVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("processTask")
@Api(value = "流程任务接口", tags = {"用于流程任务的接口"})
public class TaskController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @PostMapping("/selectTaskByPage")
    @ApiOperation(value = "获取我的代办任务",notes = "获取我的代办任务", httpMethod = "POST")
    @PreFuncAuth("PROCESS:INSTANCE:SELECT")
    public JSONResult selectTaskByPage(@Valid @RequestBody SelectTaskByPageBO selectTaskByPageBO){
        Integer pageNum = selectTaskByPageBO.getPageNum();
        Integer pageSize = selectTaskByPageBO.getPageSize();
        int firstResult = pageNum*pageSize-pageSize;
        int maxResult = pageNum*pageSize-1;
        String username = SecurityUtils.getUsername();
        TaskQuery query =taskService.createTaskQuery()
                .taskAssignee(username)
                .orderByTaskCreateTime()
                .desc();
        List<Task> taskList = query.listPage(firstResult,maxResult);
        List<ProcessTaskVO> processTaskVOList = new ArrayList<>();
        for (org.flowable.task.api.Task task:taskList){
            if(task.getAssignee() != null){
                ProcessTaskVO processTaskVO = new ProcessTaskVO();
                processTaskVO.setId(task.getId());
                processTaskVO.setName(task.getName());
                processTaskVO.setStatus("");
                ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
                processTaskVO.setInstanceName(processInstance.getName());
                processTaskVO.setCreatedDate(task.getCreateTime());
                processTaskVO.setAssignee(task.getAssignee());
                processTaskVOList.add(processTaskVO);
            }
        }
        Map<String,Object> result = PageUtils.getResultTableData(processTaskVOList);
        return JSONResult.ok(result);
    }

    @PostMapping("/selectPickupTaskByPage")
    @ApiOperation(value = "获取我的拾取任务",notes = "获取我的拾取任务", httpMethod = "POST")
    @PreFuncAuth("PROCESS:INSTANCE:SELECT")
    public JSONResult selectPickupTaskByPage(@Valid @RequestBody SelectTaskByPageBO selectTaskByPageBO){
        Integer pageNum = selectTaskByPageBO.getPageNum();
        Integer pageSize = selectTaskByPageBO.getPageSize();
        int firstResult = pageNum*pageSize-pageSize;
        int maxResult = pageNum*pageSize-1;
        String username = SecurityUtils.getUsername();
        TaskQuery query =taskService.createTaskQuery()
                .taskCandidateUser(username)
                .orderByTaskCreateTime()
                .desc();
        List<Task> taskList = query.listPage(firstResult,maxResult);
        List<ProcessTaskVO> processTaskVOList = new ArrayList<>();
        for (org.flowable.task.api.Task task:taskList){
            if(task.getAssignee() != null){
                ProcessTaskVO processTaskVO = new ProcessTaskVO();
                processTaskVO.setId(task.getId());
                processTaskVO.setName(task.getName());
                processTaskVO.setStatus("");
                ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
                processTaskVO.setInstanceName(processInstance.getName());
                processTaskVO.setCreatedDate(task.getCreateTime());
                processTaskVO.setAssignee(task.getAssignee());
                processTaskVOList.add(processTaskVO);
            }
        }
        Map<String,Object> result = PageUtils.getResultTableData(processTaskVOList);
        return JSONResult.ok(result);
    }

    @PostMapping("/completeTask")
    @ApiOperation(value = "完成我的代办任务",notes = "完成我的代办任务", httpMethod = "POST")
    @PreFuncAuth("PROCESS:INSTANCE:COMPLETE")
    public JSONResult completeTask(@Valid @RequestBody CompleteTaskBO completeTaskBO){
        Task task = taskService.createTaskQuery().taskId(completeTaskBO.getTaskId()).singleResult();
        HashMap<String, Object> variables = new HashMap<>();
        // 设置局部变量
        variables.put("pass",completeTaskBO.getPass());
        taskService.addComment(task.getId(), task.getProcessInstanceId(), completeTaskBO.getComment());
        taskService.setVariablesLocal(task.getId(),variables);
        taskService.complete(task.getId());
        return JSONResult.ok("审核完成");
    }
}
